package com.winkyle.woodenfish.presenter;


import com.winkyle.woodenfish.controller.activity.LoginActivity;
import com.winkyle.woodenfish.entity.HttpResult;
import com.winkyle.woodenfish.mvpview.LoginView;
import com.winkyle.woodenfish.retrofit.UserInfoRetrofit;
import com.winkyle.woodenfish.rx.HttpSubscriber;

/**
 * @author winkyle
 * @date 2018/06/13
 * @description  活动类 {@link LoginActivity}的p层
 */

public class LoginPresenter extends BaseMvpPresenter<LoginView>  {

    /**
     * @description 通过参数account，password向服务器请求登录，若登录成功
     *               则调用v层登录成功方法，若失败则弹出提示框，告知原因
     * @param account 账户
     * @param password 密码
     * @return
     */
    public void login(String account, String password){
        if (account==null||"".equals(account)){
            getMvpView().showRemindDialog("账号不能为空");
            return;
        }
        if (password==null||"".equals(password)){
            getMvpView().showRemindDialog("密码不能为空");
            return;
        }
        subscribeToRequest(createRetrofitApi(UserInfoRetrofit.class).findLoginResult(getDeviceNumber(),account,password,"android"),
                new HttpSubscriber<LoginPresenter,HttpResult>(this) {
                    @Override
                    public void processData(HttpResult httpResult) {
                        if(httpResult.getCode() == 1){
                            getMvpView().loginSucceed();
                            return;
                        }
                        getMvpView().showRemindDialog(httpResult.getMsg());
                    }
                });

    }
}
